<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/16 0016
 * Time: 23:20
 */

namespace App\Repositories\Admin;


use App\Models\Admin\Picture;

class PictureRepository
{
    /*
     * @ 通过图片id查询图片信息
     * @ 返回图片相关的完整信息
     */
    public function getById($id){
        return Picture::where('picture_id',$id)->first();
    }
    /*
     * @ 查询所有图片信息
     * @ 返回图片相关的完整信息
     */
    public function getAll($pictureType = false){
	if(!$pictureType)
            return Picture::get();
        else
            return Picture::where('picture_type',$pictureType)->get();
    }
    /**
     * 获取某种类型所有通过审核的图片
     */
    public function getMatchAll($pictureType = false){
        if(!$pictureType)
            return Picture::get();
        else
            return Picture::where('picture_type',$pictureType)->where('picture_check','1')->get();
    }
    /*
     * @ 通过多条件查询图片
     */
    public function getByMore($where){
        return Picture::where($where)->with('pictures')->get();
    }
    /*
     * @ 通过多条件查询图片 或条件
     */
    public function getMatchPicture($ids){
        return Picture::where('picture_id','=',$ids[0])->orwhere('picture_id','=',$ids[1])->with('pictures')->get();
    }
    /*
     * @ 通过多条件分页查询图片
     */
    public function getByMorePage($where,$number){
        return Picture::where($where)->with('pictures')->simplePaginate($number);
    }
    /*
     * @ 通过用户id查询图片
     */
    public function getByUserId($userId){
        return Picture::where('picture_user_id',$userId)->with(['rank'=>function ($rank) {
            $rank->where('rank_type',2);
        }])->get();
    }

    /**
     * 管理获取图片
     */
    public function getManagePicture($group,$orderby,$page,$pageNumber){
        if($group != 'all'){
            $pictures = Picture::where('picture_type',$group)
                ->orderBy('created_at', $orderby)
                ->with('pictures')->simplePaginate($pageNumber);
            $total = Picture::where('picture_type',$group)->count();
            return ['pictures'=>$pictures,'total'=>$total];
        }else{
            $pictures =  Picture::orderBy('created_at', $orderby)
                ->with('pictures')->simplePaginate($pageNumber);
            $total = $this->gerNumber();
            return ['pictures'=>$pictures,'total'=>$total];
        }
    }

    /*
     * @ 查询图片总数
     */
    public function gerNumber(){
        return Picture::count();
    }
    /**
     * 分页
     */
    public function page($number){
        $users = Picture::simplePaginate($number);
        return $users;
    }
    /**
     * @ 添加图片的信息
     * @ 返回的是 User模型对象
     */
    public function createPicture($picture){
        return Picture::create($picture);
    }
    /**
     * 修改picture_check字段的值
     */
    public function updateCheck($id,$value){
        $picture = Picture::find($id);
        $picture->picture_check = $value;
        return $picture->save();
    }
    /**
     * 修改picture_likenum字段的值
     */
    public function updateLikenum($id,$value){
        $picture = Picture::find($id);
        $picture->picture_likenum = $value;
        return $picture->save();
    }
    /**
     * 修改picture_dislikenum字段的值
     */
    public function updateDislikenum($id,$value){
        $picture = Picture::find($id);
        $picture->picture_dislikenum = $value;
        return $picture->save();
    }

    /**
     * 修改picture_status字段的值
     */
    public function updateStatus($id,$value){
        $picture = Picture::find($id);
        $picture->picture_status = $value;
        return $picture->save();
    }
    /**
     * 修改picture_health_point字段的值
     */
    public function updateHealthPoint($id,$value){
        $picture = Picture::find($id);
        $picture->picture_health_point = $value;
        return $picture->save();
    }
    public function clearUserMatchRdis(Request $request){
        $oppenId = $request->get('oppenId');
        if(!Redis::get($oppenId)) {
            Redis::del($oppenId);
            return json_encode(['code'=>200,'msg'=>'删除成功']);
        }else
            return json_encode(['code'=>200,'msg'=>'没有数据可删除']);
    }

    //删除图片
    public function delPicture($id){
        return Picture::where('picture_id','=',$id)
            ->delete();
    }

}
